package com.zhika.zishi.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

import com.zhika.zishi.R;
import com.zhika.zishi.util.ActivityCollector;

/**
 * Created by shi-02 on 2015/5/2.
 */
public abstract class BaseFragmentActivity extends Activity {

    //刚开始进入activity时，显示的fragment
    public abstract Fragment createFragment();

    //该activity 处理fragment的逻辑等功能
    public abstract void doThing();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_fragment_layout);
        ActivityCollector.addActivity(this);


        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.main_fragment);
        if (fragment == null){
            fragment = createFragment();
            if (fragment!= null){

                fragmentManager.beginTransaction().add(R.id.main_fragment,fragment).commit();

                // 该activity 处理fragment的逻辑等功能
                doThing();
            }

        }



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.remoteActivity(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    /**
     * 启动Fragment
     *
     * @param oldFragment
     * @param newFragment
     */
    public void startFragment(Fragment oldFragment, Fragment newFragment) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        transaction.hide(oldFragment);
        transaction.add(R.id.main_fragment, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}
